﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using COFE.Core;
using COFE.Core.Utils;
using COFE.Core.Entry;

namespace COFE.Web
{
    public class ResourceList : SupportLink, ISupportXml, ISupportLinkTemplates
    {
        #region Constructor

        public ResourceList(Guid id, IResourceList resourceList)
        {
            _id = id;
            _resourceList = resourceList;
        }        

        #endregion

        #region Methods

        public XElement ToXml(bool indentLinks = false)
        {
            var entryXml = new XElement(XNames.ResourceList, 
                new XAttribute(XNames.Type, EnumUtils.GetResourceName(_resourceList.ParentResource).ToLower())
                );

            writeLinks(entryXml, indentLinks);

            return entryXml;
        }

        public override IEnumerable<Link> GetLinks()
        {
            return this.CreateLinks();
        }

        public IEnumerable<LinkTemplate> GetLinkTemplates()
        {
            foreach (var property in _resourceList.Resources)
            {
                ResourceAttribute ra = EnumUtils<ResourceAttribute>.FindAttribute(property);
                if (ra != null)
                {
                    MediaType mediaType = ra.MediaType;
                    yield return new LinkTemplate(mediaType, property.ToString(),
                        ServiceConstants.Templates.Entry + "/" + EnumUtils.GetResourceName(property).ToLower());
                }
            }
        }

        public Dictionary<string, string> GetLinkParameterDic()
        {
            return new Dictionary<string, string>()
            {
                 { "#id#", _id.ToString() }
            };
        }


        #endregion

        #region Data

        private Guid _id;
        private IResourceList _resourceList;        

        #endregion

        #region Public Properties

        #endregion
    }

}
